package api_base

import (
	"github.com/gin-gonic/gin"
	"strconv"
	"wrblog-api-go/app/common/token"
	"wrblog-api-go/app/model/model_base"
	"wrblog-api-go/app/service/service_base"
	"wrblog-api-go/pkg/request"
	"wrblog-api-go/pkg/result"
	"wrblog-api-go/pkg/status"
)

// @Tags  Base - 文章管理
// @Summary  获取文章列表
// @Accept json
// @Produce json
// @Param Authorization header string true "Authorization"
// @Param baseArticle query model_base.SelectBaseArticle true "baseArticle"
// @Success 200 {object} result.Result "OK"
// @Router /base/baseArticle/page [get]
func ApiArticlePageList(c *gin.Context) {
	raw, _ := c.Get(request.BindObj)
	baseArticle := raw.(model_base.SelectBaseArticle)
	result.PageRes(service_base.GetArticlePageList(baseArticle)).Json(c)
}

// @Tags  Base - 文章管理
// @Summary  获取当前用户的文章列表
// @Accept json
// @Produce json
// @Param Authorization header string true "Authorization"
// @Param baseArticle query model_base.SelectBaseArticle true "baseArticle"
// @Success 200 {object} result.Result "OK"
// @Router /base/baseArticle/myPage [get]
func ApiMyArticlePageList(c *gin.Context) {
	raw, _ := c.Get(request.BindObj)
	baseArticle := raw.(model_base.SelectBaseArticle)
	baseArticle.UserId = token.GetUserId(c)
	result.PageRes(service_base.GetArticlePageList(baseArticle)).Json(c)
}

// @Tags  Base - 文章管理
// @Summary  获取文章详情
// @Accept json
// @Produce json
// @Param Authorization header string true "Authorization"
// @Param articleId path int true "articleId"
// @Success 200 {object} result.Result "OK"
// @Router /base/baseArticle/{articleId} [get]
func ApiArticleInfo(c *gin.Context) {
	articleId, err := strconv.Atoi(c.Param("articleId"))
	if err != nil {
		result.Wrap(status.BadRequest, "参数读取失败").Json(c)
	}
	result.Res(service_base.GetArticleById(articleId)).Json(c)
}

// @Tags  Base - 文章管理
// @Summary  添加文章
// @Accept json
// @Produce json
// @Param Authorization header string true "Authorization"
// @Param baseArticle body model_base.BaseArticle true "baseArticle"
// @Success 200 {object} result.Result "OK"
// @Router /base/baseArticle/add [post]
func ApiAddArticle(c *gin.Context) {
	raw, _ := c.Get(request.BindObj)
	baseArticle := raw.(model_base.BaseArticlePo)
	loginUser := token.GetLoginUser(c)
	baseArticle.UserId = loginUser.UserId
	baseArticle.CreateBy = loginUser.Uk
	result.Res(service_base.AddArticle(baseArticle)).Json(c)
}

// @Tags  Base - 文章管理
// @Summary  修改文章
// @Accept json
// @Produce json
// @Param Authorization header string true "Authorization"
// @Param baseArticle body model_base.BaseArticle true "baseArticle"
// @Success 200 {object} result.Result "OK"
// @Router /base/baseArticle/edit [put]
func ApiEditArticle(c *gin.Context) {
	raw, _ := c.Get(request.BindObj)
	baseArticle := raw.(model_base.BaseArticlePo)
	baseArticle.UpdateBy = token.GetUk(c)
	result.Res(service_base.EditArticle(baseArticle)).Json(c)
}

// @Tags  Base - 文章管理
// @Summary  删除文章（逻辑删除）
// @Accept json
// @Produce json
// @Param Authorization header string true "Authorization"
// @Param articleIds query []int true "articleIds"
// @Success 200 {object} result.Result "OK"
// @Router /base/baseArticle/removes [delete]
func ApiRemoveArticle(c *gin.Context) {
	articleIds := c.Query("articleIds")
	result.Res(service_base.RemoveArticle(articleIds, token.GetUk(c))).Json(c)
}
